﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using VitalShining.WebControls;

namespace CARMS.Message
{
    public partial class MessageList :PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageQueryAdapter1.PerformQuery();
            }
        }


        protected void gridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (G100Entity)e.Row.DataItem;

                var linkPickUp = e.Row.FindControl("linkPickUp") as HyperLink;
                var reportHeader = ReportBusiness.GetReportHeaderEntityByReportNumber(entity.G1001);
                if (reportHeader != null)
                {
                    linkPickUp.NavigateUrl = SiteUrls.Report.GenerateReport + "?ReportHeaderID=" + reportHeader.ReportHeaderId + "&ReportCategoryID=" + reportHeader.ReportCategoryId;
                }
                else
                {
                    linkPickUp.Visible = false;
                }
            }
        }

        protected void gridList_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {

          

            }
        }

        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            PredicateExpression Condition = new PredicateExpression();

            //if (txtLoginName.Text.Trim().Length > 0)
            //{
            //    Condition.Add(UserFields.LoginName % ("%" + txtLoginName.Text.Trim() + "%"));
            //}
            //if (txtFullName.Text.Trim().Length > 0)
            //{
            //    Condition.Add(UserFields.FullName % ("%" + txtFullName.Text.Trim() + "%"));
            //}
            //if (chlRoles.SelectedValue.Trim().Length > 0)
            //{
            //    var userRoleCollection = new UserRoleCollection();
            //    userRoleCollection.GetMulti(UserRoleFields.RoleId == new Guid(chlRoles.SelectedValue));

            //    var list = from item in userRoleCollection select item.UserId;

            //    var compareExpression = new FieldCompareRangePredicate(UserFields.UserId, false, list.ToArray());
            //    Condition.Add(compareExpression);
            //}
            //Condition.Add(UserFields.IsActived == true); 

            Condition.Add(G100Fields.G1005 == string.Empty |
                          G100Fields.G1005%("%" + CurrentLoginUser.LoginName + "%"));
            e.Condition = Condition;
        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            var collection = new G100Collection();
    
            e.RecordCount = collection.GetDbCount((PredicateExpression)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            SortExpression sort;
            switch (gridList.SortExpression)
            {
                //case "LoginName":
                //    sort = new SortExpression(UserFields.LoginName | (SortOperator)(int)gridList.SortDirection);
                //    break;
                //case "FullName":
                //    sort = new SortExpression(UserFields.FullName | (SortOperator)(int)gridList.SortDirection);
                //    break;
                //case "Phone":
                //    sort = new SortExpression(UserFields.Tel | (SortOperator)(int)gridList.SortDirection);
                //    break;

                //case "CreateDate":
                //    sort = new SortExpression(UserFields.CreateDate | (SortOperator)(int)gridList.SortDirection);
                //    break;
                //case "Status":
                //    sort = new SortExpression(UserFields.IsActived | (SortOperator)(int)gridList.SortDirection);
                //    break;
                default:
                    sort = new SortExpression(G100Fields.CreateDate | SortOperator.Descending);
                    break;
            }

            //var collection = new MessageCollection();
            //var relation = new RelationCollection {MessageEntity.Relations.ReportHeaderEntityUsingReportHeaderId};
            //collection.GetMulti((PredicateExpression)e.Condition, 0, sort, relation, null, e.PageIndex + 1, e.PageSize);
            //e.DataSource = collection;

            var collection = new G100Collection();
    
            collection.GetMulti((PredicateExpression)e.Condition, 0, sort, null, null, e.PageIndex + 1, e.PageSize);
            e.DataSource = collection;

        }

    }
}
